#include "mainwindow.h"
#include "qlistwidget.h"
#include "ui_mainwindow.h"
#include "qmenu.h"
#include "qstring.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindowClass())
{
    ui->setupUi(this);

    //设置工具按钮行为
    ui->toolButton_Init->setDefaultAction(ui->action_Init);
    ui->toolButton_Clear->setDefaultAction(ui->action_Clear);
    ui->toolButton_Append->setDefaultAction(ui->action_Append);
    ui->toolButton_Insert->setDefaultAction(ui->action_Insert);
    ui->toolButton_Delete->setDefaultAction(ui->action_Delete);
    ui->toolButton_SelectAll->setDefaultAction(ui->action_SelectAll);
    ui->toolButton_SelectNone->setDefaultAction(ui->action_SelectNone);
    ui->toolButton_SelectInvert->setDefaultAction(ui->action_SelectInvert);

    //有菜单的工具按钮
    QMenu* menuSelection = new QMenu(this);
    menuSelection->addAction(ui->action_SelectAll);
    menuSelection->addAction(ui->action_SelectNone);
    menuSelection->addAction(ui->action_SelectInvert);
    ui->toolButton_SelectItem->setPopupMode(QToolButton::MenuButtonPopup);//设置弹出方式
    ui->toolButton_SelectItem->setMenu(menuSelection);//加入菜单

    QToolButton* aBtn = new QToolButton(this);
    aBtn->setPopupMode(QToolButton::InstantPopup);
    aBtn->setText("选择选项");
    ui->mainToolBar->addWidget(aBtn);

    ui->mainToolBar->addAction(ui->action_Quit);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_action_Init_triggered()
{
    QListWidgetItem* aItem;
    ui->listWidget->clear();

    bool chk = ui->checkBox_ListEditable->isChecked();
    for (int i = 0; i < 10; i++)
    {
        aItem = new QListWidgetItem();
        aItem->setText("Item" + QString::number(i));
        aItem->setCheckState(Qt::Checked);
        if (chk)
        {
            aItem->setFlags(Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
        }
        else
        {
            aItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
        }
        ui->listWidget->addItem(aItem);
    }
}

void MainWindow::on_action_Clear_triggered()
{
    ui->listWidget->clear();
}

void MainWindow::on_action_Insert_triggered()
{
    QListWidgetItem* aItem= new QListWidgetItem();

    bool chk = ui->checkBox_ListEditable->isChecked();    

    aItem->setText("Item Inserted");
    aItem->setCheckState(Qt::Checked);
    if (chk)
    {
        aItem->setFlags(Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
    }
    else
    {
        aItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
    }
    ui->listWidget->insertItem(ui->listWidget->currentRow(), aItem);

}

void MainWindow::on_action_Append_triggered()
{
    QListWidgetItem* aItem = new QListWidgetItem();

    bool chk = ui->checkBox_ListEditable->isChecked();
    
    aItem->setText("Item Inserted");
    aItem->setCheckState(Qt::Checked);
    if (chk)
    {
        aItem->setFlags(Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
    }
    else
    {
        aItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
    }
    ui->listWidget->addItem(aItem);
}

void MainWindow::on_action_Delete_triggered()
{
    int row = ui->listWidget->currentRow();//当前选择的行
    ui->listWidget->takeItem(row);//拿走项
}

void MainWindow::on_action_SelectAll_triggered()
{
    int cnt = ui->listWidget->count();
    QListWidgetItem* aItem = nullptr;
    for (int i = 0; i < cnt; i++)
    {
        aItem = ui->listWidget->item(i);
        aItem->setCheckState(Qt::Checked);
    }
}

void MainWindow::on_action_SelectNone_triggered()
{
    int cnt = ui->listWidget->count();
    QListWidgetItem* aItem = nullptr;
    for (int i = 0; i < cnt; i++)
    {
        aItem = ui->listWidget->item(i);
        aItem->setCheckState(Qt::Unchecked);
    }
}

void MainWindow::on_action_SelectInvert_triggered()
{
    int cnt = ui->listWidget->count();
    QListWidgetItem* aItem = nullptr;
    for (int i = 0; i < cnt; i++)
    {
        aItem = ui->listWidget->item(i);
        if(aItem->checkState()== Qt::Checked)
            aItem->setCheckState(Qt::Unchecked);
        else
            aItem->setCheckState(Qt::Checked);
    }
}

void MainWindow::on_listWidget_currentItemChanged(QListWidgetItem* current, QListWidgetItem* previous)
{
    QString str;
    if (current != nullptr)
    {
        if (previous != nullptr)
            str = "前一项：" + previous->text() + "当前项：" + current->text();            
        else
            str = "当前项：" + current->text();

        ui->lineEdit->setText(str);
    }

    ui->plainTextEdit->appendPlainText("currentItemChanged信号发送");
    
}

void MainWindow::on_checkBox_Sorting_clicked(bool checked)
{
    ui->listWidget->setSortingEnabled(checked);
}

void MainWindow::on_toolButton_Ascending_clicked()
{
    if(ui->listWidget->isSortingEnabled())
        ui->listWidget->sortItems(Qt::AscendingOrder);
}

void MainWindow::on_toolButton_Descending_clicked()
{
    if (ui->listWidget->isSortingEnabled())
        ui->listWidget->sortItems(Qt::DescendingOrder);
}

void MainWindow::on_toolButton_ClearText_clicked()
{
    ui->plainTextEdit->clear();
}

void MainWindow::on_toolButton_AddLine_clicked()
{
    ui->plainTextEdit->appendPlainText("");
}

void MainWindow::on_listWidget_customContextMenuRequested(const QPoint& pos)
{
    QMenu* menuList = new QMenu;
    menuList->addAction(ui->action_Init);
    menuList->addAction(ui->action_Clear);
    menuList->addAction(ui->action_Insert);
    menuList->addAction(ui->action_Append);
    menuList->addAction(ui->action_Delete);
    menuList->addSeparator();
    menuList->addAction(ui->action_SelectAll);
    menuList->addAction(ui->action_SelectNone);
    menuList->addAction(ui->action_SelectInvert);

    menuList->exec(QCursor::pos());//执行菜单

    delete menuList;
}
